package com.jk.service.impl;


import com.jk.mapper.LogAndRegMapper;
import com.jk.service.LogAndRegService;
import com.jk.utile.Send;
import com.jk.vo.UserInfo;
import com.jk.vo.UserLoginLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("logAndRegService")
public class LogAndRegServiceImpl implements LogAndRegService {

    @Autowired
    public LogAndRegMapper logAndRegMapper;
    @Autowired
    private Send send;


    /*登陆*/
    public Map userLogin(UserInfo user,String pcIp) {
        if(logAndRegMapper.checkNumberOrId(user)==1){
            UserInfo checkOutUser = logAndRegMapper.checkNumberAndPwd(user);
            if(null!=checkOutUser){
                System.out.println("状态"+checkOutUser.getUserStatus());
                //以下为进行 登陆 IP地址检测
                UserLoginLog userLog=new UserLoginLog();
                userLog.setLogUserId(checkOutUser.getUserId());
                userLog.setLogLoginIp(pcIp);
                int userLogCount = logAndRegMapper.checkIp(userLog);
                if(userLogCount<1){
                    send.sendMsg(userLog);
                    Map m=new HashMap();
                    m.put("e",3);
                    m.put("t",checkOutUser.getUserType());
                    m.put("s",checkOutUser.getUserStatus());
                    return m; //登陆成功，登陆Ip为陌生Ip,需进行安全验证
                }else{

                    //调用消息队列工具类  发送 登陆信息 进行记录
                    send.sendMsg(userLog);
                    Map m=new HashMap();
                    m.put("e",4);
                    m.put("t",checkOutUser.getUserType());
                    m.put("s",checkOutUser.getUserStatus());
                    return m;//登陆成功，直接登陆
                }
            }else{
                Map m=new HashMap();
                m.put("e",2);
                m.put("t",0);
                m.put("s",0);
                return m; //密码错误
            }
        }else{
            Map m=new HashMap();
            m.put("e",1);
            m.put("t",0);
            m.put("s",0);
            return m; //用户不存在
        }
    }





    /*账号/Id 是否存在*/
    public int checkNumberOrId(UserInfo user){
        int count = logAndRegMapper.checkNumberOrId(user);
        if(count==1){
            return 1;//账号/Id 已存在
        }else {
            return 0;////账号/Id 不存在
        }
    }


    public Map inserUserLogin(UserInfo user) {
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        user.setUserId(id);
        user.setRegistetime(new Date());
        int i = logAndRegMapper.insertUser(user);
        Map m=new HashMap();
        m.put("i",1);
        m.put("id",id);


        return m;
    }




}
